package net.bhzyxy.jyjsq;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.appcompat.widget.AppCompatTextView;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private AppCompatButton yi;
    private AppCompatButton er;
    private AppCompatButton san;
    private AppCompatButton si;
    private AppCompatButton wu;
    private AppCompatButton liu;
    private AppCompatButton qi;
    private AppCompatButton ba;
    private AppCompatButton jiu;
    private AppCompatButton l;
    private AppCompatButton ll;
    private AppCompatButton dian;
    private AppCompatButton jian;
    private AppCompatButton jia;
    private AppCompatButton chu;
    private AppCompatButton cheng;
    private AppCompatButton dy;
    private AppCompatTextView pm;
    private boolean c=false;
    private String jia1,jia2="";
    private double num1,num2=0;
    private String op;
    private double jieguo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        yi = findViewById(R.id.yi);
        er = findViewById(R.id.er);
        san = findViewById(R.id.san);
        si = findViewById(R.id.si);
        wu = findViewById(R.id.wu);
        liu = findViewById(R.id.liu);
        qi = findViewById(R.id.qi);
        ba = findViewById(R.id.ba);
        jiu = findViewById(R.id.jiu);
        l = findViewById(R.id.l);
        ll = findViewById(R.id.ll);
        jia = findViewById(R.id.jia);
        jian = findViewById(R.id.jian);
        cheng = findViewById(R.id.cheng);
        chu = findViewById(R.id.chu);
        pm = findViewById(R.id.pm);
        dian = findViewById(R.id.dian);
        dy = findViewById(R.id.dy);

        yi.setOnClickListener(this);
        er.setOnClickListener(this);
        san.setOnClickListener(this);
        si.setOnClickListener(this);
        wu.setOnClickListener(this);
        liu.setOnClickListener(this);
        qi.setOnClickListener(this);
        ba.setOnClickListener(this);
        jiu.setOnClickListener(this);
        l.setOnClickListener(this);
        ll.setOnClickListener(this);
        jia.setOnClickListener(this);
        jian.setOnClickListener(this);
        cheng.setOnClickListener(this);
        chu.setOnClickListener(this);
        dy.setOnClickListener(this);
        pm.setOnClickListener(this);
        dian.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
      switch (view.getId()){
          case R.id.yi:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"1");
              break;

          case R.id.er:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"2");
              break;

          case R.id.san:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"3");
              break;

          case R.id.si:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"4");
              break;

          case R.id.wu:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"5");
              break;

          case R.id.liu:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"6");
              break;

          case R.id.qi:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"7");
              break;

          case R.id.ba:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"8");
              break;

          case R.id.jiu:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"9");
              break;

          case R.id.l:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"0");
              break;

          case R.id.ll:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+"00");
              break;

          case R.id.dian:
              if (c){
                  pm.setText("");
                  c=false;
              }
              pm.setText(pm.getText().toString()+".");
              break;

          case R.id.jia:
              jia1 =pm.getText().toString();
              if (jia1.equals("")){
                  break;
              }
              num1 = Double.parseDouble(jia1);
              pm.setText("");
              op = "+";
              c = false;
              break;

          case R.id.jian:
              jia1 =pm.getText().toString();
              if (jia1.equals("")){
                  break;
              }
              num1 = Double.parseDouble(jia1);
              pm.setText("");
              op = "-";
              c = false;
              break;

          case R.id.cheng:
              jia1 =pm.getText().toString();
              if (jia1.equals("")){
                  break;
              }
              num1 = Double.parseDouble(jia1);
              pm.setText("");
              op = "*";
              c = false;
              break;

          case R.id.chu:
              jia1 =pm.getText().toString();
              if (jia1.equals("")){
                  break;
              }
              num1 = Double.parseDouble(jia1);
              pm.setText("");
              op = "/";
              c = false;
              break;

          case R.id.dy:
              jia2 = pm.getText().toString();
              if (jia2.equals("")){
                  break;
              }
              num2 = Double.parseDouble(jia2);
              pm.setText("");
              switch (op) {
                  case"+":
                      jieguo = num1+num2;
                      break;

                  case"-":
                      jieguo = num1-num2;
                      break;

                  case"*":
                      jieguo = num1*num2;
                      break;

                  case "/":
                      if(num2==0){
                          break;
                      }
                      jieguo = num1/num2;
                      break;
                  default:jieguo = 0.0;
                      break;
              }
              pm.setText(jieguo+"");
              op = "";
              c = true;
              break;


      }
    }
}